package com.origon360.converters;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.origon.entities.Currency;
import com.origon360.Services.CommonService;

@FacesConverter(value="currencyConverter")
@ManagedBean(name="currencyConverter")
@SessionScoped
public class CurrencyConverter implements Converter {
	
	@EJB
	private CommonService commonService;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value == null || value.isEmpty())
		{
			return null;
		}
		
		Currency currency = commonService.findCurrencyByName(value);
		return currency;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if(value == null)
		{
			return null;
		}
		
		if(value instanceof Currency)
		{
			return ((Currency)value).getName();
		}
		
		return value.toString();
	}

}
